﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using ConfeccionesJR.Entites;
using ConfeccionesJR.BussinesLogic;

namespace ConfeccionesJR.UI
{
    public partial class FrmProforma : Form
    {
        [Dependency]
        public IProformaBL ProformaBL { get; set; }

        [Dependency]
        public IDetalleProformaBL detProformaBL { get; set; }

        public FrmProforma()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void ReiniciarFechas()
        {
            dateTimePicker1.Value = DateTime.Now.Date;
            var fec2 = dateTimePicker1.Value.Date;
            dateTimePicker2.Value = fec2.AddDays(1);
        }

        private void CargaData()
        {
            if (rbtnGeneral.Checked) // solo cliente
            {
                dateTimePicker1.Enabled = false;
                dateTimePicker2.Enabled = false;
                dateTimePicker2.Visible = true;
                lblFec2.Visible = true;
                lblFec1.Text = "Desde:";

                pROFORMABindingSource.DataSource = ProformaBL.GetFromProformasbyFechasAndCliente(null, null, txtCriterio.Text);
            }

            if (rbtnFechas.Checked == true) // cliente y fechas
            {
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;
                dateTimePicker2.Visible = true;
                lblFec1.Text = "Desde:";
                lblFec2.Visible = true;

                var fec1 = dateTimePicker1.Value.Date;
                var fec2 = dateTimePicker2.Value.Date;

                if (fec1 <= fec2)
                {
                    pROFORMABindingSource.DataSource = ProformaBL.GetFromProformasbyFechasAndCliente(fec1, fec2, txtCriterio.Text);
                }
                else
                {
                    ReiniciarFechas();
                    MessageBox.Show("La fecha de inicio debe ser menor o igual a la fecha de término", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            if (rbtnDia.Checked == true) // cliente y dia 
            {
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Visible = false;
                lblFec2.Visible = false;
                lblFec1.Text = "Día:";

                var fec1 = dateTimePicker1.Value.Date;

                pROFORMABindingSource.DataSource = ProformaBL.GetFromProformasbyFechasAndCliente(fec1, null, txtCriterio.Text);
            }


            var _currentProforma = pROFORMABindingSource.Current as PROFORMA;
            {
                if (_currentProforma != null)
                {
                    dETALLEPROFORMABindingSource.DataSource = detProformaBL.GetFromDetalleProformaByProductoDescAndProformaId("", _currentProforma.Id);
                }
                else
                {
                    dETALLEPROFORMABindingSource.Clear();
                }
            }

            //var criterio = txtCriterio.Text == string.Empty ? null : txtCriterio.Text;
            //pROFORMABindingSource.DataSource = ProformaBL.GetAllFromProformasByCriterio(criterio);
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            var frmProforma = new frmMttProforma();
            frmProforma.AddProforma();
            frmProforma.ShowDialog();
            CargaData();
        }


        private void FrmProforma_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            lblFec1.Text = "Desde:";
            ReiniciarFechas();

            CargaData();
        }

        private void pROFORMADataGridView_CurrentCellChanged(object sender, EventArgs e)
        {
            PROFORMA currentProforma = pROFORMABindingSource.Current as PROFORMA;
            if (currentProforma != null)
            {
                dETALLEPROFORMABindingSource.DataSource = detProformaBL.GetAllFromDetalleProformaByProformaId(currentProforma.Id);
            }
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CargaData();
            }
        }

        private void TxtProductoDesc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BuscarEnDetalleCompra();
            }
        }

        private void BtnBuscarProducto_Click(object sender, EventArgs e)
        {
            BuscarEnDetalleCompra();
        }

        private void BuscarEnDetalleCompra()
        {
            PROFORMA _currentProformA = pROFORMABindingSource.Current as PROFORMA;

            if (_currentProformA != null)
            {
                dETALLEPROFORMABindingSource.DataSource = detProformaBL.GetFromDetalleProformaByProductoDescAndProformaId(TxtProductoDesc.Text, _currentProformA.Id);
            }
            else
            {
                MessageBox.Show("Debe seleccionar una Proforma", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void rbtnGeneral_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void rbtnFechas_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void rbtnDia_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void btnReporte_Click(object sender, EventArgs e)
        {
            var _proforma = pROFORMABindingSource.Current as PROFORMA;

            if (_proforma != null)
            {
                frmRepProforma frm = new frmRepProforma();
                frm.ObtenerId(_proforma.Id);
                frm.ShowDialog();

            }
            else MessageBox.Show("Debe seleccionar una Proforma", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void txtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            var slash = Convert.ToString("/");
            var coma = Convert.ToString(",");
            var guion = Convert.ToString("-");
            var punto = Convert.ToString(".");
            var amberson = Convert.ToString("&");

            string cadena = slash + coma + guion + punto + amberson + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar) || (char.IsNumber(e.KeyChar)))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void TxtProductoDesc_KeyPress(object sender, KeyPressEventArgs e)
        {
            var slash = Convert.ToString("/");
            var coma = Convert.ToString(",");
            var guion = Convert.ToString("-");
            var punto = Convert.ToString(".");

            string cadena = slash + coma + guion + punto + (char)8;
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var _proforma = pROFORMABindingSource.Current as PROFORMA;

            if (_proforma != null)
            {
                var frm = new frmMttProforma();
                frm.Editar(_proforma.Id);
                frm.ShowDialog();
                CargaData();
            }
            else
            {
                MessageBox.Show("Debe de seleccionar una Proforma");
            }
        }

        

        
    }
}
